/*
 * pps_bom
 */
package com.pitop.product.ctrl;

import com.alibaba.fastjson.JSON;
import com.pitop.core.tools.StringTools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;
import java.util.HashMap;

import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.product.entity.ProductBom;
import com.pitop.product.service.ProductBomSV;

/**
 * 销售bom关联 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/productBom")
@Slf4j
@Api(value = "销售bom关联控制器", description = "销售bom关联控制器")
public class ProductBomCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private ProductBomSV productBomSV;

    /**
     * 查询ProductBom一个详情信息
     *
     * @param id
     * @param productCode 成品编码
     * @param bomCode     bom编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询ProductBom一个详情信息", notes = "查询ProductBom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "productCode", value = "成品编码", dataType = "java.lang.String", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public ProductBom load(Long id, String productCode, String bomCode) {
        if (id == null && StringTools.isEmpty(productCode) && StringTools.isEmpty(bomCode)) {
            return null;
        }
        ProductBom productBom = productBomSV.load(id, productCode, bomCode);
        log.info(JSON.toJSONString(productBom));
        return productBom;
    }

    /**
     * 根据条件productCode查询ProductBom一个详情信息
     *
     * @param productCode 成品编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询ProductBom一个详情信息", notes = "查询ProductBom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productCode", value = "成品编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/productCode/{productCode}")
    @ResponseBody
    public ProductBom loadByProductCode(@PathVariable String productCode) {
        if (productCode == null) {
            return null;
        }
        ProductBom productBom = productBomSV.loadByProductCode(productCode);
        log.info(JSON.toJSONString(productBom));
        return productBom;
    }

    /**
     * 根据条件bomCode查询ProductBom一个详情信息
     *
     * @param bomCode bom编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询ProductBom一个详情信息", notes = "查询ProductBom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bomCode", value = "bom编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/bomCode/{bomCode}")
    @ResponseBody
    public ProductBom loadByBomCode(@PathVariable String bomCode) {
        if (bomCode == null) {
            return null;
        }
        ProductBom productBom = productBomSV.loadByBomCode(bomCode);
        log.info(JSON.toJSONString(productBom));
        return productBom;
    }

    /**
     * 根据条件id查询ProductBom一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询ProductBom一个详情信息", notes = "查询ProductBom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/get/id/{id}")
    @ResponseBody
    public ProductBom getById(@PathVariable Long id) {
        if (id == null) {
            return null;
        }
        ProductBom productBom = productBomSV.getById(id);
        log.info(JSON.toJSONString(productBom));
        return productBom;
    }

    /**
     * 根据条件productCode查询ProductBom一个详情信息
     *
     * @param productCode 成品编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询ProductBom一个详情信息", notes = "查询ProductBom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productCode", value = "成品编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/get/productCode/{productCode}")
    @ResponseBody
    public ProductBom getByProductCode(@PathVariable String productCode) {
        if (productCode == null) {
            return null;
        }
        ProductBom productBom = productBomSV.getByProductCode(productCode);
        log.info(JSON.toJSONString(productBom));
        return productBom;
    }

    /**
     * 根据条件bomCode查询ProductBom一个详情信息
     *
     * @param bomCode bom编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询ProductBom一个详情信息", notes = "查询ProductBom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bomCode", value = "bom编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/get/bomCode/{bomCode}")
    @ResponseBody
    public ProductBom getByBomCode(@PathVariable String bomCode) {
        if (bomCode == null) {
            return null;
        }
        ProductBom productBom = productBomSV.getByBomCode(bomCode);
        log.info(JSON.toJSONString(productBom));
        return productBom;
    }

    /**
     * 查询ProductBom信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询ProductBom信息集合", notes = "查询ProductBom信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
    })
    @PostMapping(value = "/list")
    @ResponseBody
    public List<ProductBom> list(@RequestBody @ApiIgnore ProductBom productBom, Integer curPage, Integer pageSize) {
        Page<ProductBom> page = new Page<ProductBom>(pageSize, curPage);
        List<ProductBom> productBoms = productBomSV.list(productBom, page.genRowStart(), page.getPageSize());
        int total = productBomSV.count(productBom);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return productBoms;
    }

    /**
     * 查询ProductBom信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询ProductBom信息集合", notes = "查询ProductBom信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "productCode", value = "成品编码", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public List<ProductBom> listByPk(Long id, String productCode, String bomCode, Integer curPage, Integer pageSize) {
        Page<ProductBom> page = new Page<ProductBom>(pageSize, curPage);
        List<ProductBom> productBoms = productBomSV.list(id, productCode, bomCode, page.genRowStart(), page.getPageSize());
        int total = productBomSV.count(id, productCode, bomCode);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return productBoms;
    }

    /**
     * 统计ProductBom信息数量根据主键
     *
     * @return 总条数
     */
    @ApiOperation(value = " 统计ProductBom信息数量根据主键", notes = " 统计ProductBom信息数量根据主键")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "productCode", value = "成品编码", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query")
    })
    @GetMapping(value = "/count/by")
    @ResponseBody
    public Integer count(Long id, String productCode, String bomCode) {
        return productBomSV.count(id, productCode, bomCode);
    }

    /**
     * 统计ProductBom信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计ProductBom信息数量", notes = "统计ProductBom信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "productCode", value = "成品编码", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query")
    })
    @PostMapping(value = "/count")
    @ResponseBody
    public Integer count(@RequestBody ProductBom productBom) {
        return productBomSV.count(productBom);
    }

    /**
     * 创建ProductBom
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建ProductBom", notes = "创建ProductBom")
    @ApiImplicitParams({
    })
    @PostMapping("/build")
    @ResponseBody
    public ProductBom build(@RequestBody @ApiIgnore ProductBom productBom) {
        productBomSV.save(productBom);
        return productBom;
    }

    /**
     * 修改ProductBom
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改ProductBom", notes = "修改ProductBom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "productCode", value = "成品编码", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public ProductBom modify(@RequestBody @ApiIgnore ProductBom productBom) {
        productBomSV.modify(productBom);
        return productBom;
    }

    /**
     * 删除ProductBom
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除ProductBom", notes = "删除ProductBom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "productCode", value = "成品编码", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String productCode, String bomCode) {
        productBomSV.delete(id, productCode, bomCode);
        return BeanRet.create(true, "删除ProductBom成功");
    }

}
